﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections;
namespace Pan
{
    public class Cookie
    {
        public enum CookieKey
        {
            www,
            bbs,
            user,
            group,
            video,
            blog
        }
        #region new cookie
        /////  
        ///// Cookie作用域 
        /////  
        //public enum CookieDomain
        //{
        //    Private,		///本网站Cookie 
        //    Public			///全网Cookie 
        //}
     
        //#region Cookie操作类
        /////  
        ///// 设置本网站Cookie值 
        /////  
        ///// Cookie作用域 
        ///// Cookie名称 
        ///// Cookie值 
        //public static void WriteCookie(string cookieName, string cookieValue)
        //{
        //    CookieDomain cookieDomain = CookieDomain.Public;
        //    DateTime cookieExpire = DateTime.MaxValue;
        //    WriteCookie(cookieDomain, cookieName, cookieValue, cookieExpire);
        //}
        /////  
        ///// 设置Cookie值 
        /////  
        ///// Cookie作用域 
        ///// Cookie名称 
        ///// Cookie值 
        //public static void WriteCookie(CookieDomain cookieDomain, string cookieName, string cookieValue)
        //{
        //    DateTime cookieExpire = DateTime.MaxValue;
        //    WriteCookie(cookieDomain, cookieName, cookieValue, cookieExpire);
        //}
        /////  
        ///// 设置Cookie值
        /////  
        ///// Cookie作用域 
        ///// Cookie名称 
        ///// Cookie值 
        ///// Cookie过期时间 
        //public static void WriteCookie(CookieDomain cookieDomain, string cookieName, string cookieValue, int days)
        //{
        //    DateTime cookieExpire = DateTime.Now.AddDays(days);
        //    WriteCookie(cookieDomain, cookieName, cookieValue, cookieExpire);
        //}
        /////  
        ///// 设置Cookie值 
        /////  
        ///// Cookie作用域 
        ///// Cookie名称 
        ///// Cookie值 
        ///// Cookie过期时间 
        //public static void WriteCookie(CookieDomain cookieDomain, string cookieName, string cookieValue, DateTime cookieExpire)
        //{
        //    cookieValue = System.Web.HttpUtility.UrlEncode(cookieValue);
        //    //cookieName = cookieDomain.ToString() + cookieName;
        //    cookieName = System.Web.HttpUtility.UrlEncode(cookieName);
        //    HttpCookie cookie = new HttpCookie(cookieName.ToLower(), cookieValue);
        //   // cookie.Domain = HttpContext.Current.Request.Url.Host;
        //    if (!cookieExpire.Equals(DateTime.Now))
        //        cookie.Expires = cookieExpire;
        //    HttpContext.Current.Response.AppendCookie(cookie);
        //   // WebHelper.Pages.Log.WriteLog("写入COOKIE:" + cookieName.ToLower() + "|" + cookieValue);
        //}
        /////  
        ///// 取得本网站指定Cookie值，没有值返回null 
        /////  
        ///// 指定Cookie名称 
        /////  
        //public static string GetCookie(string cookieName)
        //{

        //    return GetCookie(CookieDomain.Private, cookieName, null);
        //}
        /////  
        ///// 取得本网站指定Cookie值 
        /////  
        ///// 指定Cookie名称 
        ///// 默认值 
        /////  
        //public static string GetCookie(string cookieName, string defValue)
        //{
        //    return GetCookie(CookieDomain.Private, cookieName, defValue);
        //}
        /////  
        ///// 取得指定Cookie值 
        /////
        ///// 指定Cookie名称 
        ///// 默认值 
        /////
        //public static string GetCookie(CookieDomain cookieDomain, string cookieName, string defValue)
        //{
        //    if (string.IsNullOrEmpty(cookieName))
        //        return defValue;
        //    //cookieName = cookieDomain.ToString() + cookieName;
        //    cookieName = cookieName.ToLower();
        //    cookieName = System.Web.HttpUtility.UrlEncode(cookieName);
        //   // WebHelper.Pages.Log.WriteLog("读取COOKIE---" + cookieName + ":" + System.Web.HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[cookieName].Value));
        //    if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[cookieName] != null)
        //    {
        //        //HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
        //        //cookie.Domain = HttpContext.Current.Request.Url.Host;

        //        //return System.Web.HttpUtility.UrlDecode(cookie.Value);
        //        return System.Web.HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[cookieName].Value);
        //    }
        //    else
        //    {
        //        //WebHelper.Pages.Log.WriteLog("COOKIES为空");
        //        return defValue;
        //    }
        //}

        //public static List<string> GetCookieNames(CookieDomain cookieDomain)
        //{
        //    List<string> l = new List<string>();
        //    foreach (string cookiename in HttpContext.Current.Request.Cookies.AllKeys)
        //    {
        //        if (cookiename.StartsWith(cookieDomain.ToString().ToLower()) && cookiename.Length > cookieDomain.ToString().Length)
        //        {
        //            l.Add(cookiename.Substring(cookieDomain.ToString().Length + 1));
        //        }
        //    }
        //    return l;
        //}
        //public static int GetCookieCount(CookieDomain cookieDomain)
        //{
        //    List<string> l = GetCookieNames(cookieDomain);
        //    return l.Count;
        //}
        /////  
        ///// 删除本网站指定Cookie 
        /////  
        /////  
        //public static void Clear(string cookiename)
        //{
        //    Clear(CookieDomain.Public, cookiename);
        //}
        /////  
        ///// 删除指定网站指定Cookie 
        /////  
        /////  
        /////  
        //public static void Clear(CookieDomain cookieDomain, string cookiename)
        //{
        //    WriteCookie(cookieDomain, cookiename, null, DateTime.MinValue);
        //}
        /////  
        ///// 清除指定域的Cookie 
        /////  
        ///// 指定域 
        //public static void Clear(CookieDomain cookieDomain)
        //{
        //    foreach (string cookiename in HttpContext.Current.Request.Cookies.AllKeys)
        //    {
        //        WriteCookie(cookieDomain, cookiename, null, DateTime.MinValue);
        //    }
        //}
        /////  
        ///// 清除所有域的Cookie 
        /////  
        //public static void Clear()
        //{
        //    Clear(CookieDomain.Private);
        //    Clear(CookieDomain.Public);
        //}
        /////  
        ///// 取得对应CookieDomain的作用域 
        /////  
        /////  
        /////  
        //public static string GetCookieDomain(CookieDomain cookiedomain)
        //{
        //    switch (cookiedomain)
        //    {
        //        case CookieDomain.Private:
        //            string tmp = HttpContext.Current.Request.Url.Host;
        //            if (tmp == "user." + DomainStr)
        //                tmp = "." + DomainStr;
        //            return tmp;
        //            break;
        //        case CookieDomain.Public:
        //            return "." + DomainStr;
        //            break;
        //        default:
        //            return "." + DomainStr;
        //            break;

        //    }
        //}
        //#endregion
        #endregion

        #region 旧的Cookie类



        public static void WriteCookie(string strName, string strValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            //cookie.Path = "/";
            //cookie.Domain = "5151tuan.com";
            cookie.Value = System.Web.HttpUtility.UrlEncode(strValue);
            cookie.Expires = DateTime.MaxValue;
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        ///  
        /// 写cookie值 
        ///  
        /// 名称 
        /// 值 
        public static void WriteCookie(string strName, string strValue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie == null)
            {
                cookie = new HttpCookie(strName);
            }
            cookie.Path = "/";
            cookie.Domain = "5151tuan.com";
            cookie.Value = System.Web.HttpUtility.UrlEncode(strValue);
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        ///  
        /// 读cookie值 
        ///  
        /// 键 
        /// 默认的返回值 
        /// 名称 
        /// cookie值 
        public static string GetCookie(string strName, string defValue)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
            {
                return System.Web.HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value);
            }

            return defValue;
        }

        ///  
        /// 读cookie值 
        ///  
        /// 键 
        /// 默认的返回值 
        /// 名称 
        /// cookie值 
        public static string GetCookie(string strName)
        {
            string defValue = null;
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
            {
                return System.Web.HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value);
            }

            return defValue;
        }


        #endregion

        #region 有子键的操作
       
        ///  
        /// 读cookie值 
        ///  
        /// 键 
        /// 名称 
        /// cookie值
        public static string GetCookie(CookieKey strDomain, string strName, string defValue)
        {
            //string domain = SetDomain(strDomain); 
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
            {
                return HttpContext.Current.Request.Cookies[strName].Value;
            }
            return defValue;
        }

        #endregion
    }
}

